package com.qixin.activiti.listenter;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qixin.activiti.service.IEaCommunicationsBXNCItfService;
import com.qixin.activiti.service.IEaTravelBXNCItfService;
import com.qixin.activiti.utils.EaListenerUtil;
import com.qixin.modules.ea.entity.EaBookDetail;
import com.qixin.modules.ea.entity.EaTravel;
import com.qixin.modules.ea.service.IEaBookDetailService;
import com.qixin.modules.ea.service.IEaTravelService;
import com.qixin.modules.eaBasic.service.IEaRegionService;
import com.qixin.modules.log.entity.SysInterfaceLog;
import com.qixin.modules.log.service.ISysInterfaceLogService;
import com.qixin.modules.rzInvoice.service.IEaBillService;
import com.qixin.modules.thread.ActivitiThread;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.jeecg.common.nc.enums.WSEnum;
import org.jeecg.common.nc.service.NCWebService;
import org.jeecg.common.nc.vo.WSVO;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.entity.ActZTask;
import org.jeecg.modules.activiti.service.IActZTaskService;
import org.jeecg.modules.activiti.service.Impl.ActBusinessServiceImpl;
import org.jeecg.modules.nc.service.INcBdPsnjobService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/***
* @Description: 差旅费用报销流程节点监听器
* @author: CYS
* @date: 2022/3/15 11:40
*/
@Slf4j
@Service
public class EaTravelExecutionListener implements ExecutionListener, TaskListener {
    // 流程业务扩展表
    private ActBusinessServiceImpl actBusinessService;

    private IEaTravelBXNCItfService iEaTravelBXNCItfService;
    private ISysInterfaceLogService logServiceImpl;



    @Override
    public void notify(DelegateExecution execution) throws Exception {
        //事件名称
        String eventName = execution.getEventName();

        //测试
        /*String eventName ="start";*/

        //获取业务表相关信息
        String procInstId = execution.getProcessInstanceId(); // 流程实例Id
        String actBusinessId = execution.getProcessBusinessKey(); // 获取业务扩展表Id

        //获取审批人信息 end
        QueryWrapper<ActBusiness> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",actBusinessId);
        actBusinessService = (ActBusinessServiceImpl) SpringContextUtils.getBean("actBusinessServiceImpl");
        ActBusiness actBusiness = actBusinessService.getOne(queryWrapper);
        log.info("actTableName:{}",actBusiness.getTableName());

        //测试
          /*  ActBusiness actBusiness = new ActBusiness();
            actBusiness.setTableId("1504012421629284353");
        actBusiness.setTitle("CLBX202246631647419479--");*/






            /*
          进入start相关事件调用WSDL在做处理
            */
        //流程审批结束后执行相应的业务操作，比如把单据推送到ERP
        if ("start".equals(eventName)) {
            String billNo=actBusiness.getTitle().substring(0,actBusiness.getTitle().indexOf("--"));

            ActivitiThread activitiThread = new ActivitiThread( actBusiness, billNo, eventName, "eaCommunications");
            activitiThread.start();
            System.out.println("start=========");
        }else if ("end".equals(eventName)) {
            System.out.println("end=========");
        }
        else if ("take".equals(eventName)) {
            System.out.println("take=========");
        }

    }

    @Override
    public void notify(DelegateTask delegateTask) {
        String eventName = delegateTask.getEventName();
        if ("create".endsWith(eventName)) {
            System.out.println("create=========");
        }else if ("assignment".endsWith(eventName)) {
            System.out.println("assignment========");
        }else if ("complete".endsWith(eventName)) {
            System.out.println("complete===========");
        }else if ("delete".endsWith(eventName)) {
            System.out.println("delete=============");
        }
    }
}
